package com.bot.devbot.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.bot.devbot.service.feishu.FeiShuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Nexus仓库变更通知接收控制器
 * <a href="http://nexus.annaji.com.cn:8081/#admin/system/capabilities">Nexus Webhook配置地址</a>
 * <p>配置地址hook地址：http://172.19.11.44:9999/nexus-webhook/deploy</p>
 *
 * @author xialiguo
 * @version 1.0
 * @date 2025/2/14 15:46
 * @since 1.0
 */
@Slf4j
@RestController
@RequestMapping("/nexus-webhook")
public class NexusWebhookController {

    @Autowired
    private FeiShuService feiShuService;

    @PostMapping("/deploy")
    public String handleDeployNotification(@RequestBody String payload) {
        // 解析Webhook发送的JSON消息
        JSONObject json = new JSONObject(payload);

        log.info("Received Nexus Webhook Notification: {}", json);
        // 提取关键信息
        String action = json.getStr("action");
        String repositoryName = json.getStr("repositoryName");
        // action 为UPDATED直接返回
        boolean needNotice = true;
        if ("UPDATED".equals(action)) {
            needNotice = false;
        }

        // 如果是maven-releases仓库，也提醒
        if ("maven-releases".equals(repositoryName)) {
            needNotice = true;
        }

        if (!needNotice) {
            return "Received";
        }

        String timestamp = json.getStr("timestamp");
        String initiator = json.getStr("initiator");
        String message = "";
        // 判断是否是组件信息
        String url = "http://nexus.annaji.com.cn:8081/#browse/browse:maven-releases:com/danale";
        if (json.containsKey("component")) {
            JSONObject component = json.getJSONObject("component");
            String componentName = component.getStr("name");
            String componentVersion = component.getStr("version");
            String componentGroup = component.getStr("group");
            String componentId = component.getStr("id");
            url = getNexusUrl(repositoryName, componentGroup, componentName);
            message = String.format("Nexus仓库: %s\n操作人: %s\n操作类型: %s\n组件名称: %s\n版本: %s\n组名: %s\n组件ID: %s\n时间戳: %s",
                    repositoryName, initiator, action, componentName, componentVersion, componentGroup, componentId, timestamp);
        }
        // 如果是资产信息
        else if (json.containsKey("asset")) {
            // 如果是maven-releases仓库的asset提醒，替他不提醒
            if (!"maven-releases".equals(repositoryName)) {
                return "Received";
            }
            JSONObject asset = json.getJSONObject("asset");
            String assetName = asset.getStr("name");
            String assetId = asset.getStr("id");

            if (!(StrUtil.endWith(assetName, ".pom") || StrUtil.endWith(assetName, ".jar"))) {
                return "Received";
            }

            message = String.format("Nexus仓库: %s\n操作类型: %s\n资源名称: %s\n资源ID: %s\n时间戳: %s",
                    repositoryName, action, assetName, assetId, timestamp);
        }
        feiShuService.sendMessagePostNexus(message, url);
        return "Received";
    }

    public String getNexusUrl(String repositoryName, String groupId, String artifactId) {
        String baseUrl = "http://nexus.annaji.com.cn:8081";
        return String.format("%s/#browse/browse:%s:%s/%s", baseUrl, repositoryName, groupId.replace('.', '/'), artifactId);
    }
}
